let uiWaitTime = 1000;
function clickUI(item, waitTime) {
    if (item) {
        if (item.clickable()) {
            item.click();
        } else {
            click(item.bounds().centerX(), item.bounds().centerY());
        }
        if (waitTime && waitTime > 0) {
            // console.log('睡眠' + waitTime)
            sleep(waitTime);
        }
    } else {
        console.warn("UI控件为空，无法点击");
    }
}

function __openApp__() {
    // 启动app
    app.startActivity({
        action: "android.intent.action.MAIN",
        packageName: "com.jingdong.app.mall",
        className: "com.jingdong.app.mall.main.MainActivity",
        category: ["android.intent.category.LAUNCHER"],
        flags: ["activity_new_task"]
    });
    sleep(uiWaitTime * 3);    
}
// 打开关注店铺页面
function __openConcernedShopPage__() {
    // 打开【我的】标签页
    let btn = text('我的').find()[0];
    clickUI(btn, uiWaitTime);
    console.log('打开【我的】标签页');
    btn = text('店铺关注').find()[0];
    clickUI(btn, uiWaitTime);
    console.log('打开【店铺关注】页面');
}
// 取消店铺关注，返回是否还有关注店铺
function __cancleAttention__() {
    // 进入编辑模式
    let btn = text('编辑').find()[0];
    if (!btn) {
        console.log('没有关注的店铺');
        return false;
    }
    clickUI(btn, uiWaitTime);
    console.log('点击【编辑】按钮');
    // 全选已展示的店铺
    btn = text('全选').find()[0];
    clickUI(btn, uiWaitTime);
    console.log('点击【全选】按钮');
    // 取消关注
    btn = text('取消关注').depth(13).find()[0];
    clickUI(btn, uiWaitTime);
    console.log('点击【取消关注】按钮');
    // 确定
    btn = text('确定').find()[0];
    clickUI(btn, uiWaitTime);
    console.log('点击【确定】按钮');
    sleep(uiWaitTime);
    return true;
}
function run() {
    __openApp__();
    __openConcernedShopPage__();
    let count = 0;
    while(__cancleAttention__()) {
        console.log('第' + ++count + '次取消关注');
    }

}
run()